package com.hgk.common.common.ui.activity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.common.dongqiuhui.apps.comm.net.BallAsyncTask;
import com.common.dongqiuhui.apps.comm.util.DataResult;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase;
import com.common.handmark.pulltorefresh.library.PullToRefreshListView;
import com.common.handmark.pulltorefresh.library.PullToRefreshZoomListView;
import com.common.handmark.pulltorefresh.library.internal.BallBaseAdapter;
import com.common.nostra13.universalimageloader.core.DisplayImageOptions;
import com.common.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import width.hgk.wanwan.R;

/**
 * 上拉下拉刷新框架
 * 
 * @author fyh
 * 
 */
public abstract class HomeUpAndDownRefreshActivity extends
		BallPhotoToolActivity implements PullToRefreshBase.OnRefreshListener2<ListView>,
		OnItemClickListener {

	public PullToRefreshZoomListView mPullRefreshListView;
	public DrawerLayout mDrawerLayout;
	private AsyncTask<String, Integer, DataResult> dataAsyncTask;

	private BallBaseAdapter mBaseAdapter;

	public BallBaseAdapter getmBaseAdapter() {
		return mBaseAdapter;
	}

	private HomeUpAndDownRefreshActivity.PullStyle mPullStyle;
	private ImageLoader mImageLoader;
	private DisplayImageOptions displayImageOptionPic;
	public TextView tv_timeline_title;//时光轴标题
	private int count = 0;
	private long lastClickTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mImageLoader = ImageLoader.getInstance();
		//displayImageOptionPic = LoadImageDisplayOption
			//	.getDefaultPic(R.drawable.load_start);
		mPullRefreshListView = (PullToRefreshZoomListView) findViewById(R.id.pull_refresh_list);
		mPullRefreshListView.setShowIndicator(false);
		//mPullRefreshListView.getRefreshableView().setSelector(
				//mContext.getResources().getDrawable(R.drawable.trans));
		//setDivider(R.color.listline);
		setDividerHeight(2);
		mPullRefreshListView.setMode(onCreate());
		initAdapter(getAdapte());
		onCreateEnd();
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
		//tv_timeline_title = (TextView) findViewById(R.id.tv_timeline_title);
		// 双击回到顶部
//		findViewById(R.id.timeline_title_view).setOnClickListener(
//				new OnClickListener() {
//					@Override
//					public void onClick(View v) {
//						count++;
//						long currentTime = System.currentTimeMillis();
//						long dTime = currentTime - lastClickTime;
//						lastClickTime = currentTime;
//						if (count >= 2 && dTime < 2000) {
//							mPullRefreshListView.getRefreshableView()
//									.setSelection(0);
//							count = 0;
//						}
//					}
//		});

	}



	/**
	 * 初始化 adapter
	 *
	 * @param adapter
	 */
	private void initAdapter(BallBaseAdapter adapter) {
		this.mBaseAdapter = adapter;
		mPullRefreshListView.setAdapter(mBaseAdapter);
		mPullRefreshListView.setOnRefreshListener(this);
		mPullRefreshListView.setOnItemClickListener(this);
	}

	/**
	 * 设置item颜色
	 *
	 * @param imgId
	 */
	protected final void setDivider(int imgId) {
		if (imgId == 0) {
			mPullRefreshListView.getRefreshableView().setDivider(null);
		} else {
			mPullRefreshListView.getRefreshableView().setDivider(
					this.getResources().getDrawable(imgId));
		}
	}

	/**
	 * 设置item之间的DividerHeight
	 *
	 * @param height
	 */
	protected final void setDividerHeight(int height) {
		mPullRefreshListView.getRefreshableView().setDividerHeight(height);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		onDownRefresh();
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		onUpRefresh();
	}

	/**
	 * 设置适配器
	 *
	 * @return
	 */
	protected abstract BallBaseAdapter getAdapte();

	/**
	 * 初始化
	 *
	 */
	protected abstract PullToRefreshBase.Mode onCreate();

	/**
	 * 加载结束
	 */
	protected void onCreateEnd() {
		onNetStart();
	};

	/**
	 * 下拉刷新事件
	 */
	protected abstract void onDownRefresh();

	/**
	 * 上拉刷新时间
	 */
	protected abstract void onUpRefresh();

	/**
	 * 加载数据
	 */
	protected final void onNetStart() {
		dataAsyncTask = new DownRefreshAsyncTask(this).execute();
	}

	/**
	 * 刷新数据
	 */
	protected final void onRefresh() {
		mBaseAdapter.notifyDataSetChanged();
		mPullRefreshListView.onRefreshComplete();
	}

	/**
	 * 设置list显示
	 *
	 * @param visibility
	 */
	protected final void setListVisibility(int visibility) {
		mPullRefreshListView.getRefreshableView().setVisibility(visibility);
	}

	/**
	 * 加载结束关闭刷新动画
	 */
	protected final void onEndRefresh() {
		mPullRefreshListView.setDataLoadingState(false);
		mPullRefreshListView.onRefreshComplete();
		mBaseAdapter.notifyDataSetChanged();
	}

	/**
	 * 清除数据
	 */
	protected final void clearData() {
		mBaseAdapter.clear();
	}

	/**
	 * 设置数据
	 *
	 * @param data
	 */
	public void setData(List data) {
		mBaseAdapter.setData(data);

	}

	public void setDataForEqual(List data) {
		mBaseAdapter.setDataForEquals(data);
	}

	public <T> void setData(T data, int position) {
		setData(data, position);
	}

	/**
	 * 尾添加一条数据
	 *
	 * @param data
	 */
	public void setDataLast(Object data) {
		mBaseAdapter.setDataLast(data);
	}

	/**
	 * 头部添加一组数据
	 *
	 * @param datas
	 */
	public void setDataToHader(List datas) {
		mBaseAdapter.setMultitermDataToHader(datas);
	}

	/**
	 * 添加头
	 *
	 * @param v
	 */
	public void addHead(View v) {
		mPullRefreshListView.getRefreshableView().addHeaderView(v);
	}

	/**
	 * 删除指定 ID
	 *
	 * @param position
	 */
	public void removeData(int position) {
		mBaseAdapter.removeData(position);
	}

	/**
	 * 删除指定 ID
	 *
	 * @param position
	 */
	public Object getData(int position) {
		return mBaseAdapter.getItem(position);
	}

	public void setDataFirst(Object data) {
		mBaseAdapter.setDataFirst(data);
	}

	/**
	 * 删除指定 t
	 *
	 * @param t
	 */
	public void removeData(Object t) {
		mBaseAdapter.removeData(t);
	}

	/**
	 * 设置listView显示在底部
	 */
	public void setSelectionFoot() {
		mPullRefreshListView.getRefreshableView().setSelection(
				mBaseAdapter.getCount() - 1);
	}

	/**
	 * 联网成功
	 *
	 * @param result
	 */
	protected abstract void success(DataResult result);

	/**
	 * 获取联网数据
	 *
	 * @return
	 */
	protected abstract DataResult getDataResult();

	/**
	 * 联网失败
	 *
	 * @param result
	 */
	protected void failure(DataResult result) {
		if (result.message.equalsIgnoreCase("")) {
			result.message = "加载失败";
		}
		onEndRefresh();
	}

	/**
	 * 设置联网请求文字 * @return
	 */
	protected String getDialogMessage() {
		return "";
	}

	/**
	 * 更多帮/圈子 列表
	 *
	 * @author pg 第几页
	 *
	 * @author wangbingqi
	 *
	 */
	final class DownRefreshAsyncTask extends BallAsyncTask {
		boolean isb = true;

		public DownRefreshAsyncTask(Context context) {
			super(context);
		}

		@Override
		protected DataResult toNet(String[] params) {
			return HomeUpAndDownRefreshActivity.this.getDataResult();
		}

		@Override
		protected void success(DataResult result) {
			//hideNetView();
			HomeUpAndDownRefreshActivity.this.success(result);
		}

		@Override
		protected void failure(DataResult result) {
			//showNetView();
			//hideLoadingView();
			HomeUpAndDownRefreshActivity.this.failure(result);
		}

		@Override
		protected String getDialogMessage() {
			return HomeUpAndDownRefreshActivity.this.getDialogMessage();
		}

	}

	@Override
	public int getBodyView() {
		return 0;
	}

	/**
	 * 获取PullToRefreshListView
	 *
	 * @return
	 */
	public PullToRefreshListView getPullRefreshListView() {
		return mPullRefreshListView;
	}

	/**
	 * 上拉加载 或者 自动加载
	 *
	 * @return
	 */
	public HomeUpAndDownRefreshActivity.PullStyle getPullStyle() {
		return HomeUpAndDownRefreshActivity.PullStyle.AUTO;
	}

	/**
	 * 加载更多方式 AUTO 自动 MANUAL 手动
	 */
	public static enum EventSource {
		AUTO, MANUAL;
	}

	/**
	 * 上下拉方式 AUTO 自动加载更多模式 MANUAL 上拉加载更多模式
	 */
	public static enum PullStyle {
		AUTO, MANUAL;
	}
}
